﻿'         This file is Copyright © 2008-2012 Billy O'Neal and Aommaster
'                            All rights reserved.
'
' This file is released under the 2-Clause BSD license, included in LICENSE.TXT
'                           and on the MHWT website
Imports System.Runtime.Serialization

<System.Serializable()> Friend Class Issue
    Private GUIDInternal As Guid
    Private EvidenceInternal As String = ""
    Private DescriptionInternal As String = ""
    Public Delegate Sub Generic()
    <System.NonSerialized()> Private dNonSerializableHandlers As New List(Of Generic)
    Private dSerializableHandlers As New List(Of Generic)
    Public Custom Event Deleted As Generic
        AddHandler(ByVal value As Generic)
            If value.Target.GetType.IsSerializable Then
                dSerializableHandlers.Add(value)
            Else
                If dNonSerializableHandlers Is Nothing Then
                    dNonSerializableHandlers = New List(Of Generic)
                End If
                dNonSerializableHandlers.Add(value)
            End If
        End AddHandler
        RemoveHandler(ByVal value As Generic)
            If value.Target.GetType.IsSerializable Then
                dSerializableHandlers.Remove(value)
            Else
                dNonSerializableHandlers.Remove(value)
            End If
        End RemoveHandler
        RaiseEvent()
            Dim temp As New List(Of Generic)
            temp.AddRange(dNonSerializableHandlers)
            For Each item As Generic In temp
                item.Invoke()
            Next
            temp.Clear()
            temp.AddRange(dSerializableHandlers)
            For Each item As Generic In temp
                item.Invoke()
            Next
            temp.Clear()
        End RaiseEvent
    End Event
    <System.NonSerialized()> Private mNonSerializableHandlers As New List(Of Generic)
    Private mSerializableHandlers As New List(Of Generic)
    Public Custom Event Modified As Generic
        AddHandler(ByVal value As Generic)
            If value.Target.GetType.IsSerializable Then
                mSerializableHandlers.Add(value)
            Else
                If mNonSerializableHandlers Is Nothing Then
                    mNonSerializableHandlers = New List(Of Generic)
                End If
                mNonSerializableHandlers.Add(value)
            End If
        End AddHandler
        RemoveHandler(ByVal value As Generic)
            If value.Target.GetType.IsSerializable Then
                mSerializableHandlers.Remove(value)
            Else
                mNonSerializableHandlers.Remove(value)
            End If
        End RemoveHandler
        RaiseEvent()
            Dim temp As New List(Of Generic)
            temp.AddRange(mNonSerializableHandlers)
            For Each item As Generic In temp
                item.Invoke()
            Next
            temp.Clear()
            temp.AddRange(mSerializableHandlers)
            For Each item As Generic In temp
                item.Invoke()
            Next
            temp.Clear()
        End RaiseEvent
    End Event
    Public Sub Delete()
        RaiseEvent Deleted()
    End Sub
    Public ReadOnly Property GUID() As Guid
        Get
            Return GUIDInternal
        End Get
    End Property
    Public Property Evidence() As String
        Get
            Return EvidenceInternal
        End Get
        Set(ByVal value As String)
            If value <> EvidenceInternal Then
                EvidenceInternal = value
                RaiseEvent Modified()
            End If
        End Set
    End Property
    Public Property Description() As String
        Get
            Return DescriptionInternal
        End Get
        Set(ByVal value As String)
            If DescriptionInternal <> value Then
                DescriptionInternal = value
                RaiseEvent Modified()
            End If
        End Set
    End Property
    Public Sub New()
        GUIDInternal = System.Guid.NewGuid()
    End Sub
    <OnDeserialized()> Private Sub OnDeSerialized(ByVal context As StreamingContext)
        dNonSerializableHandlers = New List(Of Generic)
        dSerializableHandlers = New List(Of Generic)
        mNonSerializableHandlers = New List(Of Generic)
        mSerializableHandlers = New List(Of Generic)
        If EvidenceInternal Is Nothing Then EvidenceInternal = ""
        If DescriptionInternal Is Nothing Then DescriptionInternal = ""
    End Sub
    Public Overrides Function ToString() As String
        Dim retString As String
        If Description.IndexOf(ControlChars.NewLine) <> -1 Then
            retString = Description.Substring(0, Description.IndexOf(ControlChars.NewLine))
        Else
            retString = Description
        End If
        If retString.Length > 80 Then
            retString = retString.Substring(0, 77)
            retString += "..."
        End If
        Return retString
    End Function
End Class
